﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class AddEquipmentChange : EFViewCmes
    {
        public AddEquipmentChange(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.fdType.IsEnabled = false;
                    this.fdEquipmentId.IsEnabled = false;
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertPanel.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 设备改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null && !picker.ItemObject.Equals(""))
            {
                this.fdEquipmentCode.Value = picker.ItemObject.GetType().GetProperty("fdCode").GetValue(picker.ItemObject, null);
                this.fdEquipmentId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                this.fdOldDepartmentId.Value = picker.ItemObject.GetType().GetProperty("fdDepartmentId").GetValue(picker.ItemObject, null);
                this.fdOldEngineeringId.Value = picker.ItemObject.GetType().GetProperty("fdEngineeringId").GetValue(picker.ItemObject, null);
                this.fdOldDepartment.Value = picker.ItemObject.GetType().GetProperty("fdDepartment").GetValue(picker.ItemObject, null);
                this.fdOldEngineering.Value = picker.ItemObject.GetType().GetProperty("fdEngineering").GetValue(picker.ItemObject, null);
                this.fdNewDepartmentId.Value = picker.ItemObject.GetType().GetProperty("fdDepartmentId").GetValue(picker.ItemObject, null);
                this.fdNewEngineeringId.Value = picker.ItemObject.GetType().GetProperty("fdEngineeringId").GetValue(picker.ItemObject, null);
                this.fdNewDepartment.Value = picker.ItemObject.GetType().GetProperty("fdDepartment").GetValue(picker.ItemObject, null);
                this.fdNewEngineering.Value = picker.ItemObject.GetType().GetProperty("fdEngineering").GetValue(picker.ItemObject, null);
                this.fdOldStatus.Value = picker.ItemObject.GetType().GetProperty("fdUsingType").GetValue(picker.ItemObject, null);
            }
        }

        /// <summary>
        /// 现所在部门改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdNewDepartmentName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdNewDepartmentId.Value = box.SelectedItem.GetType().GetProperty("orgCode").GetValue(box.SelectedItem, null).ToString();
                this.fdNewDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null).ToString();
            }
        }

        /// <summary>
        /// 现所在工程改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdNewEngineeringName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdNewEngineeringId.Value = box.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(box.SelectedItem, null).ToString();
                this.fdNewEngineering.Value = box.SelectedItem.GetType().GetProperty("fdName").GetValue(box.SelectedItem, null).ToString(); 
            }
        }

        /// <summary>
        /// 变动类型改变，给设备的选择添加一些条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            CmesCustomView picker = this.fdEquipmentId.Content as CmesCustomView;
            picker.ItemObject = "";
            this.fdOldEngineeringId.Value = "";
            this.fdOldDepartmentId.Value = "";
            this.fdEquipmentCode.Value = "";
            if ("领用".Equals(e.NewValue.ToString()))
            {
                this.fdUsingType.Value = "在用";
                //领取闲置和封存的设备
                picker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=recipients:1";
                this.fdNewDepartmentId.IsEnabled = true;
                this.fdNewEngineeringId.IsEnabled = true;
                this.fdNewDepartmentId.eLabel = "*现所在部门";
                this.fdNewDepartmentId.eRegex = "^.+$";
                this.fdNewDepartmentId.eValidateErrorPrompt = "不能为空！";
                return;
            }
            if ("启封".Equals(e.NewValue.ToString()))
            {
                this.fdUsingType.Value = "在用";
                //启封封存的设备
                picker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=fdUsingType:封存";
                this.fdNewDepartmentId.IsEnabled = true;
                this.fdNewEngineeringId.IsEnabled = true;
                this.fdNewDepartmentId.eLabel = "*现所在部门";
                this.fdNewDepartmentId.eRegex = "^.+$";
                this.fdNewDepartmentId.eValidateErrorPrompt = "不能为空！";
                return;
            }
            if ("封存".Equals(e.NewValue.ToString()))
            {
                this.fdUsingType.Value = "封存";
                //封存在用和闲置的设备
                picker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=archive:1";
                this.fdNewDepartmentId.IsEnabled = false;
                this.fdNewEngineeringId.IsEnabled = false;
                this.fdNewDepartmentId.eLabel = "现所在部门";
                this.fdNewDepartmentId.eRegex = "";
                this.fdNewDepartmentId.eValidateErrorPrompt = "";
                return;
            }
            if ("移交".Equals(e.NewValue.ToString()))
            {
                this.fdUsingType.Value = "在用";
                //移交在用和闲置的设备
                picker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=devolve:1";
                this.fdNewDepartmentId.IsEnabled = true;
                this.fdNewEngineeringId.IsEnabled = true;
                this.fdNewDepartmentId.eLabel = "*现所在部门";
                this.fdNewDepartmentId.eRegex = "^.+$";
                this.fdNewDepartmentId.eValidateErrorPrompt = "不能为空！";
                return;
            }
            if ("闲置".Equals(e.NewValue.ToString()))
            {
                this.fdUsingType.Value = "闲置";
                //闲置在用的设备
                picker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=idle:1";
                this.fdNewDepartmentId.IsEnabled = false;
                this.fdNewEngineeringId.IsEnabled = false;
                this.fdNewDepartmentId.eLabel = "现所在部门";
                this.fdNewDepartmentId.eRegex = "";
                this.fdNewDepartmentId.eValidateErrorPrompt = "";
                return;
            }
            if ("报废".Equals(e.NewValue.ToString()))
            {
                this.fdUsingType.Value = e.NewValue;
                //不能选择调拨 和 报废的设备
                picker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=scrapped:1";
                this.fdNewDepartmentId.IsEnabled = false;
                this.fdNewEngineeringId.IsEnabled = false;
                this.fdNewDepartmentId.eLabel = "现所在部门";
                this.fdNewDepartmentId.eRegex = "";
                this.fdNewDepartmentId.eValidateErrorPrompt = "";
            }
        }
    }
}
